package com.zh.note.huawei.onehundred.静态扫描最优成本;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhanghuan
 * @date: 2023/8/4 10:38
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int m = sc.nextInt();
        // String idStr = sc.nextLine();
        // String sizeStr = sc.nextLine();
        int m = 5;
        String idStr = "2 2 2 2 2 5 2 2 2";
        String sizeStr = "3 3 3 3 3 1 3 3 3";
        int res = solveMethod(m, idStr, sizeStr);
        System.out.println(res);
    }

    private static int solveMethod(int m, String idStr, String sizeStr) {
        String[] ids = idStr.split(" ");
        String[] sizes = sizeStr.split(" ");
        Map<Integer, Integer> idCostMap = new HashMap<>();//存储每个id出现次数
        Map<Integer, Integer> idSizeMap = new HashMap<>();// 存储每个id大小
        // 统计id出现的次数，以及每个id的大小
        for (int i = 0; i < ids.length; i++) {
            int id = Integer.parseInt(ids[i]);
            int size = Integer.parseInt(sizes[i]);
            idCostMap.put(id, idCostMap.getOrDefault(id, 0) + 1);
            idSizeMap.put(id, size);
        }
        int sum = 0;
        for (Integer id : idCostMap.keySet()) {
            int total = idCostMap.get(id) * idSizeMap.get(id);
            idCostMap.put(id, Math.min(total, m + idSizeMap.get(id)));
            sum += idCostMap.get(id);
        }

        return sum;
    }
}
